package com.example.design.filter.context;

import com.example.design.DateUtils;
import com.example.design.filter.enums.ScheduleShiftEnum;
import lombok.Getter;

import java.time.LocalDate;
import java.util.*;

/**
 * 售前排班计划
 */
@Getter
public class PreSaleScheduleContext extends ScheduleContext {

    /**
     * 每天的白班班次 <Date(yyyyMMdd), List<ScheduleUser.class>>
     */
    private final Map<String, Set<ScheduleUser>> dayWorkMap = new HashMap<>(8);

    /**
     * 每天的晚班班次<Date(yyyyMMdd), List<ScheduleUser.class>>
     */
    private final Map<String, Set<ScheduleUser>> nightWorkMap = new HashMap<>(8);

    /**
     * 早班班次
     */
    private Integer dayShiftNum;

    public PreSaleScheduleContext(Long teamId, List<Long> shopIds, LocalDate date, Map<String, Integer> shiftMap, List<ScheduleUser> users) {
        super(teamId, shopIds, date, shiftMap, users);
    }

    @Override
    public List<String> getScheduleShifts() {
        return Arrays.asList(ScheduleShiftEnum.PRE_SALE_DAY.getDictCode(), ScheduleShiftEnum.PRE_SALE_NIGHT.getDictCode(), ScheduleShiftEnum.PRE_SALE_REST.getDictCode());
    }

    public void setDayShiftNum(Integer dayShiftNum) {
        this.dayShiftNum = dayShiftNum;
    }

    /**
     * 添加早班班次员工
     */
    public void addDayWorkUser(ScheduleUser user) {
        String dateKey = scheduleDate.format(DateUtils.FORMAT_YYYYMMDD);
        Set<ScheduleUser> dayWorkUsers = dayWorkMap.computeIfAbsent(
                dateKey, k -> new HashSet<>()
        );
        dayWorkUsers.add(user);
        user.getShiftMap().put(dateKey, ScheduleShiftEnum.PRE_SALE_DAY);
        this.dayShiftNum--;
        this.shiftNum--;
    }

    /**
     * 添加晚班班次员工
     */
    public void addNightWorkUser(ScheduleUser user) {
        String dateKey = scheduleDate.format(DateUtils.FORMAT_YYYYMMDD);
        Set<ScheduleUser> dayWorkUsers = nightWorkMap.computeIfAbsent(
                dateKey, k -> new HashSet<>()
        );
        dayWorkUsers.add(user);
        user.getShiftMap().put(dateKey, ScheduleShiftEnum.PRE_SALE_NIGHT);
        this.shiftNum--;
    }
}
